<? $this -> load -> view('header');?>

<script type="text/javascript" src="<?=base_url()?>/script/ajaxupload.3.5.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false"></script>
<script type="text/javascript" >
	$('#address_detail').hide();
	getDataAllData();
	$(function(){
		var btnUpload=$('#upload');
		var status=$('#status');
		new AjaxUpload(btnUpload, {
		action: '<?=$current_module;?>/uploadgpx',
			name: 'uploadfile',
			onSubmit: function(file, ext){
			
			if (! (ext && /^(kml|gpx)$/.test(ext))){
			// extension is not allowed
				status.text('Only KML,gpx files are allowed');
				return false;
			}
				status.text('Uploading...');
			},
			onComplete: function(file, response){
				status.text('');
				if(response==="error"){
					$('<li></li>').appendTo('#files').text(file).addClass('error');
				} else{
					$('<li></li>').appendTo('#files').text("Upload Complete").addClass('success');
					$('#land_docid').val(response);
					//$('#upload_kml').hide();
					$('#dataList').show();
					getDataAllData();
				}
			}
			});
	});
	function getDataAllData(){
		$('#content_list').html("");
			$.get("<?=$current_module?>/get_all_land", function(data){
				$('#content_list').html(data);
			});
	}

	function saveAddress () {
	if($('#landtype').val()!="0"){
		$.post("<?=$current_module?>/save_address", { housenum: $('#housenum').val()
		, village:$('#village').val(),moono:$('#moono').val(),street:$('#street').val()
		,district:$('#district').val(),city:$('#city').val(),province:$('#province').val()
		,country:$('#country').val(),zipcode:$('#zipcode').val()
		,land_docid:$('#land_docid').val(),landtype:$('#landtype').val()
		,ownerland:$('#ownerLand').val(),landid:selectLandID },
		function(data) {
			$('#land_docid').val("");
			$('#housenum').val("");
			$('#village').val("");
			$('#moono').val("");
			$('#street').val("");
			$('#district').val("");
			$('#city').val("");
			$('#province').val("");
			$('#country').val("");
			$('#zipcode').val("");
			$('#ownerLand').val("0")
		
			//	alert(data);
			$('#address_detail').hide();
		}
		);
	}else{
		alert("กรุณาเลือกประเภทพื้นที่เพาะปลูก");
	}

	}
	function backEditAddress(){
		$('#land_docid').val("");
		$('#housenum').val("");
		$('#village').val("");
		$('#moono').val("");
		$('#street').val("");
		$('#district').val("");
		$('#city').val("");
		$('#province').val("");
		$('#country').val("");
		$('#zipcode').val("");
		$('#ownerLand').val("0")
		$('#address_detail').hide();
	}
	function deleteLand (landID) {
		$.post("<?=$current_module?>/deleteLandArea", { landid:landID},
	function(data) {
		alert(data);
		// window.location.reload();
		getDataAllData();
		});
	}

	function getDataByID(addsID,seLandType){
		$('#color_map').val("000090");
		this.selectLandID = addsID;
		this.selectLandType= seLandType;
		$.get("<?=$current_module?>/getAddressByID",{landid:addsID}, function(data){
		
			$('#land_docid').val(data.land_addressid);
			$('#housenum').val(data.housenum);
			$('#village').val(data.village);
			$('#moono').val(data.moono);
			$('#street').val(data.street);
			
			$('#district').val(data.district);
			$('#city').val(data.city);
			$('#province').val(data.province);
			$('#country').val(data.country);
			
			
			$('#zipcode').val(data.zipcode);
			$('#address_detail').show();
		
			$('#landtype').val(data.landtypeid);
		
			$('#ownerLand').val(data.uuid);
			
			getGeography(data.district);
			createCountry(data.district,data.city);
			createAmphur(data.city,data.province);
			
			createdistrict(data.province,data.country);
		//	createCountry(data.district,data.district)
		
			document.getElementById("iframe_map").src ="<?=$current_module . '/map/';?>"+selectLandID;
		}, "json");
	}
	var selectLandID;
	var selectLandType;
	function createMap(){
		document.getElementById("iframe_map").src ="<?=$current_module . '/map/';?>"+selectLandID;
	}
	
	function getGeography(geoid){
		
		$('#div_geography').val("");
		$.get('<?=$current_module?>/geography', function(data) {
			var create = '<select id="country">';
			 create += '<option value="0">กรุณาเลือกภาค</option>';
			 $.each($.parseJSON(data), function() {
			 	if(geoid==this.GEO_ID){
			 		create += '<option value="'+this.GEO_ID +'" selected="selected">'+this.GEO_NAME+'</option>';
			 	}else{
                	create += '<option value="'+this.GEO_ID +'">'+this.GEO_NAME+'</option>';
               }
		     });
		     create += '</select>';
		     $('#div_geography').html(create);
		     $('#country').change(function() {		
		     		createCountry($('#country').val(),"0");
		     
			 	
			});
		});
	}
		function createCountry(geoid,provinceid){
					$('#div_province').val("");
		     		
		     		var createProvince = '<select id="province">';
			 		createProvince += '<option value="0">กรุณาเลือกจังหวัด</option>';
		     		$.get('<?=$current_module?>/province',{geoid:geoid}, function(data) {
		     			$.each($.parseJSON(data), function() {
		     				if(provinceid==this.PROVINCE_ID){
		     				 	createProvince += '<option value="'+this.PROVINCE_ID +'" selected="selected">'+this.PROVINCE_NAME	+'</option>';	
		     				}else{
				             	createProvince += '<option value="'+this.PROVINCE_ID +'">'+this.PROVINCE_NAME	+'</option>';
				            }
						 });
						createProvince += '</select>';
						$('#div_province').html(createProvince);
						   $('#province').change(function() {	
						   	//amphur
						  
								createAmphur($('#province').val(),"0");
						   });
		     		});
		}
		function createAmphur(provinceID,amphurSelectID){
			 	$('#div_amphur').show();
						   	$('#div_amphur').val("");
						
						   		$.get('<?=$current_module?>/amphur',{provinceid:provinceID}, function(data) {
						   			   	var createAmphur = '<select id="city">';
			 				createAmphur += '<option value="0">กรุณาเลือกอำเภอ</option>';
				     				$.each($.parseJSON(data), function() {
				     					//alert(this.AMPHUR_ID );
				     					if(amphurSelectID==this.AMPHUR_ID){
				     						createAmphur += '<option value="'+this.AMPHUR_ID +'" selected="selected">'+this.AMPHUR_NAME	+'</option>';
				     					}else{
						             		createAmphur += '<option value="'+this.AMPHUR_ID +'">'+this.AMPHUR_NAME	+'</option>';
						             	}
									 });
									  createAmphur += '</select>';
								
									 $('#div_amphur').html(createAmphur);
									   $('#city').change(function() {	
						 
						   					createdistrict($('#city').val(),"0");
									   });
								 });
		}
		function createdistrict(cityid,districtSelectID){
					
									   	$('#div_district').val("");
									   	var tmpJson;
									   		$.get('<?=$current_module?>/district',{amphurid:cityid}, function(data) {
									   			  	tmpJson = data;
									   			  	var createDistrict = '<select id="district">';
									 				createDistrict += '<option value="0">กรุณาเลือกเขต</option>';
										     				$.each($.parseJSON(data), function() {
										     					if(districtSelectID==this.DISTRICT_ID){
										     						 createDistrict += '<option value="'+this.DISTRICT_ID +'" selected="selected">'+this.DISTRICT_NAME	+'</option>';
										     					}else{
										     						 createDistrict += '<option value="'+this.DISTRICT_ID +'">'+this.DISTRICT_NAME	+'</option>';
										     					}
												            
															 });
															  createDistrict += '</select>';
														
															 $('#div_district').html(createDistrict);
															   $('#district').change(function() {	
															   		//$('#city').val()
															   		//alert($('#city').val());
															   		$.each($.parseJSON(tmpJson), function() {
															   			
															   			if($('#district').val()==this.DISTRICT_ID){
															   			$('#zipcode').val(this.DISTRICT_CODE)
															   			}
															   		});
															   });
									   		});
		}
	
	
	
</script>
<div id="middle">
	<? $this -> load -> view('land/left_menu');?>
	<div id="center-column">
		<div class="top-bar">
			<div class="top-bar">
				<a href="#" id="upload" class="button">Add Land </a>
				<h1>Land</h1>
				<span id="status" ></span>
				<ul id="files" ></ul>
			</div>
		</div>
		<div class="select-bar">
			<p>
				<label>
					<input type="text" name="textfield">
				</label>
				<label>
					<input type="submit" name="Submit" value="Search">
				</label>
			</p>
		</div>
		<div  id="address_detail" style="display: none;">
			<div id="container">
				<table width="100%" >
					<tr>
						<td width="ุ0%">
						<table  >
							<input type="hidden" id="land_docid" name="land_docid" />
							<tr>
								<td width="200px">Housenum:</td>
								<td>
								<input type="text" name="housenum" id="housenum" />
								</td>
							</tr>
							<tr>
								<td>Village:</td>
								<td>
								<input type="text" name="village" id="village" />
								</td>
							</tr>
							<tr>
								<td>Moono:</td>
								<td>
								<input type="text" name="moono" id="moono" />
								</td>
							</tr>
							<tr>
								<td>Street:</td>
								<td>
								<input type="text" name="street" id="street" />
								</td>
							</tr>
							<tr>
								<td>Country:</td>
								<td>
									<span id="div_geography"><select id="country"></select></span>
							
								</td>
							</tr>
						

							<tr>
								<td>Province:</td>
								<td>
									<span id="div_province"><select id="province"></select></span>
							
								</td>
							</tr>
							<tr>
								<td>City:</td>
								<td>
							
								
	<span id="div_amphur"><select id="city"></select></span>
	
								</td>
							</tr>
							<tr>
								<td>District:</td>
								<td>
									<span id="div_district"><select id="district"></select></span>
							
								</td>
							</tr>
							<tr>
								<td>Zipcode:</td>
								<td>
								<input type="text" name="zipcode" id="zipcode" />
								</td>
							</tr>
							<tr>
								<td>ประเภทพื้นที่เพาะปลูก:</td>
								<td><? echo form_dropdown('landtype', $landtype, '0', 'id="landtype"');?></td>
							</tr>
							<tr>
								<td>เจ้าของที่ดิน:</td>
								<td><? echo form_dropdown('ownerLand', $ownerLand, '0', 'id="ownerLand"');?></td>
							</tr>
							<tr>
								<td>ขนาดพื้นที่:</td>
								<td><input type="text" id="Size" style="width:100px;" /> ไร่</td>
							</tr>
							<tr>
								<td colspan="2" align="center">
								<input type="button" value="Save" onclick="saveAddress()" />
								<input type="button" value="Back" onclick="backEditAddress()" />
								</td>
							</tr>
						</table></td>
						<td width="50%"><iframe id="iframe_map" width="300px" height="300px" frameBorder="0" ></iframe></td>
					</tr>
				</table>
			</div>
		</div>
		<div class="table">
			<div style="clear: both;" id="content_list"></div>
		</div>
	</div>
	<div id="right-column" style="display: none;">
		<strong class="h">Quick Info</strong>
		<div class="box" >
			<span id="user_infor"></span>
		</div>
	</div>
</div>
<? $this -> load -> view('footer');?>
